package com.zrspring.libv2.view.adview.core;

import android.view.View;
import android.view.ViewGroup;

import com.zrspring.libv2.view.adview.ADImageViewHolder;
import com.zrspring.libv2.view.adview.ADItemClickListener;
import com.zrspring.libv2.view.adview.ADViewEntity;
import java.util.List;


/**
 * Created by Sai on 15/7/29.
 */
public class ADPageAdapter extends RecyclingPagerAdapter {
	
    protected List<ADViewEntity> mDatas;
    private ADItemClickListener divewClickListener;

    public ADPageAdapter(List<ADViewEntity> datas) {
        this.mDatas = datas;
    }

    @Override 
    public View getView(final int position, View view, final ViewGroup container) {
    	BaseHolder holder ;
        if (view == null) {
            holder = new ADImageViewHolder();
            view = holder.createView(container.getContext());
            view.setTag(holder);
        } else {
            holder = (BaseHolder) view.getTag();
        }
        if(mDatas!=null&&!mDatas.isEmpty()){
        	  holder.updateUI(container.getContext(), position, mDatas.get(position));
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(divewClickListener!=null){
                    divewClickListener.onClick(position, mDatas.get(position));
                }
            }
        });
        return view;
    }

    public void setDivewClickListener(ADItemClickListener divewClickListener) {
        this.divewClickListener = divewClickListener;
    }

    @Override
    public int getCount() {
        if(mDatas==null)return 0;
        return mDatas.size();
    }
}
